package com.anda.logistics.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.anda.logistics.map.util.JsonStatus;
import com.anda.logistics.model.SystemMenu;
import com.anda.logistics.service.AndaBaseService;
import com.anda.logistics.service.SystemMenuService;
import com.anda.logistics.util.JQGridJsonData;
import com.anda.logistics.util.JQGridPager;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage(value="manager")
public class SystemMenuAction extends AndaBaseAction implements ModelDriven<SystemMenu>{

	private static final long serialVersionUID = -7612317831224301010L;
	private SystemMenuService systemMenuService;
	private List<SystemMenu> allMenu;
	private SystemMenu systemMenu = new SystemMenu();
	private SystemMenu one;
	private String oper;
	private JQGridJsonData results;
	private JsonStatus status;
	private AndaBaseService andaBaseService;
	private JQGridPager pager;
	
	@Action(value="/manager/system/menu-input"
		,results=@Result(name=INPUT,location="system-menu-input.jsp"))
	public String input(){
		return INPUT;
	}
	
	@Action(interceptorRefs=@InterceptorRef("menuDefaultStack")
		,value="/manager/system/menu_list"
		,results=@Result(name="list",location="system-menu-list.jsp"))
	public String list(){
		return LIST;
	}
	
	@Action(value="/manager/system/menu-save"
		,results=@Result(name=SUCCESS,type="redirectAction",location="menu-one?id=${id}"))
	public String save(){
		systemMenuService.save(systemMenu);		
		return SUCCESS;
	}
	
	@Action(value="/manager/system/menu-one"
			,results=@Result(name="one",location="system-menu-one.jsp"))
	public String one(){
		one = systemMenuService.getOne(systemMenu.getId());
		return ONE;
	}
	
	@Action(value="/manager/system/system-menu-list"
		,results=@Result(name=SUCCESS,type="json",params={"root","results","contentType","text/html","enableGZIP","true","noCache","true"})
	)
	public String menuList(){
		try {
			results = andaBaseService.getJQGridJsonData(pager, "id", 
				new String[]{"id","menuName","menuUrl","sortNum","menuMain"},
				SystemMenu.class);
		} catch (Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}

	@Action(value="/manager/system/menu-edit")
	public String edit(){
		if (oper.equalsIgnoreCase("edit")){
			try {
				if (systemMenu.getMenuName() != null) systemMenu.setMenuName(URLDecoder.decode(systemMenu.getMenuName(),"UTF-8"));
				if (systemMenu.getMenuUrl() != null) systemMenu.setMenuUrl(URLDecoder.decode(systemMenu.getMenuUrl(),"UTF-8"));
				if (systemMenu.getMenuMain() != null) systemMenu.setMenuMain(URLDecoder.decode(systemMenu.getMenuMain(),"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}			
			this.systemMenuService.update(systemMenu);
		} else if (oper.equalsIgnoreCase("del")){
			this.systemMenuService.deleteOne(systemMenu.getId());
		} else if (oper.equalsIgnoreCase("add")){
			return INPUT;
		}
		return null;
	}
	
	public SystemMenuService getSystemMenuService() {
		return systemMenuService;
	}
	@Resource
	public void setSystemMenuService(SystemMenuService systemMenuService) {
		this.systemMenuService = systemMenuService;
	}

	public List<SystemMenu> getAllMenu() {
		return allMenu;
	}

	public void setAllMenu(List<SystemMenu> allMenu) {
		this.allMenu = allMenu;
	}

	@Override
	public SystemMenu getModel() {
		return systemMenu;
	}

	public SystemMenu getOne(){
		return one;
	}
	public void setOne(SystemMenu one){
		this.one = one;
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}
	
	public SystemMenu getSystemMenu() {
		return systemMenu;
	}

	public void setSystemMenu(SystemMenu systemMenu) {
		this.systemMenu = systemMenu;
	}

	public JQGridJsonData getResults() {
		return results;
	}

	public void setResults(JQGridJsonData results) {
		this.results = results;
	}

	public JsonStatus getStatus() {
		return status;
	}

	public void setStatus(JsonStatus status) {
		this.status = status;
	}

	public JQGridPager getPager() {
		return pager;
	}

	public void setPager(JQGridPager pager) {
		this.pager = pager;
	}

	public AndaBaseService getAndaBaseService() {
		return andaBaseService;
	}
	@Resource
	public void setAndaBaseService(AndaBaseService andaBaseService) {
		this.andaBaseService = andaBaseService;
	}

	public static void main(String [] args ){
		try {
			System.out.println(java.net.URLDecoder.decode("%E8%A7%92%E8%89%B2%E7%AE%A1%E7%90%86","UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
	}
}
